package dao;

import util.PageInfo;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import java.util.List;

public class BaseDAO<T> extends HibernateDaoSupport{
	private Class c;
	@Autowired
	public void setMySessionFactory( SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}
	
	public BaseDAO(Class c) {
		this.c=c;
	}
	//查询全部
	public List<T> queryAll(){
		return getHibernateTemplate().loadAll(c);
	}

	//添加
	public void save(T t) {
		getHibernateTemplate().save(t);
	}
	//根据id 查询
	public T queryById(java.io.Serializable id) {
		return (T) getHibernateTemplate().get(c, id);
	}
	
	//删除
	public void delById(java.io.Serializable id) {
		getHibernateTemplate().delete(  this.queryById(id));
	}
/*
	//高级查询
    public List<Emp> queryByLike(DetachedCriteria ca,DetachedCriteria ca2, int pageNum, int pageSize) {
		return getHibernateTemplate().executeWithNativeSession(new HibernateCallback<List<Emp>>() {
			@Override
			public List<Emp> doInHibernate(Session session) throws HibernateException {
				Criteria criteria = ca.getExecutableCriteria(session);
				int count = getCount(ca2);
				int pages=1;//共有多少页
				if(count%pageSize==0){
					pages=count/pageSize;
				}else{
					pages=count/pageSize+1;
				}
				if(pages<1){
					pages=1;
				}
				int begin=(pageNum-1)*pageSize;

				criteria.setMaxResults(pageSize).setFirstResult(begin);
				List<Emp> list = criteria.list();
				return list;
			}
		});


    }
//求条数
	public int getCount(DetachedCriteria ca){
		ca.setProjection(Projections.count(c.getDeclaredFields()[0].getName()));
		return getHibernateTemplate().executeWithNativeSession(new HibernateCallback<Integer>() {
			@Override
			public Integer doInHibernate(Session session) throws HibernateException {
				return ((Long) ca.getExecutableCriteria(session).uniqueResult()).intValue();
			}
		});
	}*/

	public PageInfo<T> queryByLike2(T t,String hql, int pageNum, int pageSize) {

		return getHibernateTemplate().execute(new HibernateCallback<PageInfo>() {
			@Override
			public PageInfo doInHibernate(Session session) throws HibernateException {
				int count=getCount(hql,t);
				int pages=1;
				if(count%pageSize==0){
					pages=count/pageSize;
				}else{
					pages=count/pageSize+1;
				}
				if(pages<1){
					pages=1;
				}
				int pageNum2=pageNum;
				if(pageNum2<1){
					pageNum2=1;
				}
				if(pageNum2>pages){
					pageNum2=pages;
				}
				int begin=(pageNum2-1)*pageSize;

				Query query= session.createQuery(hql);
				if(t!=null)
					query.setProperties(t);
				query.setMaxResults(pageSize);
				query .setFirstResult(begin);
				PageInfo info=new PageInfo();
				info.setCount(count);

				info.setList(query.list());
				info.setPages(pages);
				info.setPageNum(pageNum2);
				info.setPageSize(pageSize);
				System.out.println(info.getList());
				return info;
			}
		});
	}

	public int getCount(final String hql,T t){
		String  hql2="select count(*) "+hql;
		return getHibernateTemplate().execute(new HibernateCallback<Integer>() {
			@Override
			public Integer doInHibernate(Session session) throws HibernateException {
				Query query= session.createQuery(hql2);
				if(t!=null){
					query.setProperties(t);
				}
				return ((Long)query.uniqueResult()).intValue();
			}
		});
	}

}




